package com.songshuai.pangu.api.component;

import com.alibaba.fastjson.JSONObject;
import com.songshuai.pangu.api.controller.BaseController;
import com.songshuai.webapi.baseapi.dto.pangu.request.phpsop.PhpSopReqDto;
import com.songshuai.webapi.baseapi.utils.EncryptUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.LinkedHashMap;

/**
 * 对接php-客户活动
 * @Author: wbl
 * @Date: 2019/3/29
 */
@Slf4j
@Component
public class PhpSopComponent extends BaseController {

    @Value("${php.sop.secret}")
    private String secret;

    @Value("${php.sop.getSopTypesUrl}")
    private String sopTypesUrl;

    @Value("${php.sop.getSopListUrl}")
    private String sopListUrl;

    @Value("${php.sop.getSopDetailUrl}")
    private String sopDetailUrl;

    //根据客户id获取会议列表
    public String getSopTypes(String typeId) {
        try {
            JSONObject json = new JSONObject();
            json.put("type", typeId);
            String param = "";
            param = json.toJSONString();
            param += secret;
            log.info("参数加密前：" + param);
            param = EncryptUtil.md5(param);
            log.info("参数加密后：" + param);
            json.put("token", param);
            return callPhpSopApi(json, sopTypesUrl);
        } catch (Exception e) {
            log.error("请求php sop内容-获取分类列表异常：{}", e);
        }
        return null;
    }

    // sop内容列表
    public String getSopList(PhpSopReqDto sopReqDto){
        try {
            JSONObject json = new JSONObject(new LinkedHashMap<>());
            json.put("attr", StringUtils.hasText(sopReqDto.getAttr()) ? sopReqDto.getAttr() : "");
            json.put("page", sopReqDto.getPage());
            json.put("size", sopReqDto.getSize());
            json.put("title", StringUtils.hasText(sopReqDto.getTitle()) ? sopReqDto.getTitle() : "");
            json.put("type", StringUtils.hasText(sopReqDto.getType()) ? sopReqDto.getType() : "");
            String param = "";
            param = json.toJSONString();
            param += secret;
            log.info("参数加密前：" + param);
            param = EncryptUtil.md5(param);
            log.info("参数加密后：" + param);
            json.put("token", param);
            return callPhpSopApi(json, sopListUrl);
        } catch (Exception e) {
            log.error("请求php sop内容-获取内容列表异常：{}", e);
        }
        return null;
    }

    // sop内容列表
    public String getSopDetail(String id){
        try {
            JSONObject json = new JSONObject();
            json.put("id", id);
            String param = "";
            param = json.toJSONString();
            param += secret;
            log.info("参数加密前：" + param);
            param = EncryptUtil.md5(param);
            log.info("参数加密后：" + param);
            json.put("token", param);
            return callPhpSopApi(json, sopDetailUrl);
        } catch (Exception e) {
            log.error("请求php sop内容-获取内容详情异常：{}", e);
        }
        return null;
    }
}